package jee.demo.pro.util;


import java.util.Iterator;

/**
 * Class com.haibao.gmall.utils.StringUtils description goes here.
 *
 * @author Administrator
 * @version 1.0.1 2013-01-05 9:45
 */
public class StringUtils extends org.springframework.util.StringUtils {
    /**
     * 获取用户访问URL中的根域名.
     * 例如: www.baidu.com -> baidu.com
     *
     * @param host the host name
     * @return DomainOfServerName
     */
    public static String getDomainOfServerName(String host) {
        if (isIp(host))
            return null;
        String[] names = StringUtils.split(host, ".");
        int len = names.length;
        if (len == 1) return null;
        if (len == 3) {
            return makeup(names[len - 2], names[len - 1]);
        }
        if (len > 3) {
            String dp = names[len - 2];
            if (dp.equalsIgnoreCase("com")
                    || dp.equalsIgnoreCase("gov")
                    || dp.equalsIgnoreCase("net")
                    || dp.equalsIgnoreCase("edu")
                    || dp.equalsIgnoreCase("org"))
                return makeup(names[len - 3], names[len - 2], names[len - 1]);
            else
                return makeup(names[len - 2], names[len - 1]);
        }
        return host;
    }

    /**
     * 判断字符串是否是一个IP地址.
     *
     * @param addr a String Object
     * @return true if the input is an IP address, otherwise false.
     */
    public static boolean isIp(String addr) {
        if (StringUtils.isEmpty(addr))
            return false;
        String[] ips = StringUtils.split(addr, ".");
        if (ips.length != 4)
            return false;
        try {
            int ipa = Integer.parseInt(ips[0]);
            int ipb = Integer.parseInt(ips[1]);
            int ipc = Integer.parseInt(ips[2]);
            int ipd = Integer.parseInt(ips[3]);
            return ipa >= 0 && ipa <= 255 && ipb >= 0 && ipb <= 255 && ipc >= 0
                    && ipc <= 255 && ipd >= 0 && ipd <= 255;
        } catch (NumberFormatException ignored) {
        }
        return false;
    }

    private static String makeup(String... ps) {
        StringBuilder s = new StringBuilder();
        for (int idx = 0; idx < ps.length; idx++) {
            if (idx > 0) s.append('.');
            s.append(ps[idx]);
        }
        return s.toString();
    }

    static char convertCase(char ch) {
        return !Character.isLetter(ch) ? ch : (char) (ch ^ 0x20);
    }

    public static String convertCase(String s) {
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            chars[i] = convertCase(chars[i]);
        }
        return new String(chars);
    }

    public static String xssFilter(String s) {
        return isEmpty(s) ? s : s.replaceAll("(&|<|>|'|\")" , "");
    }

    public static String xssConvert(String s) {
        return isEmpty(s) ? s :
               s.replaceAll("&", "&amp;")
                .replaceAll("<", "&lt;")
                .replaceAll(">", "&gt;")
                .replaceAll("\"", "&#34;")
                .replaceAll("'", "&#39;");
    }

    public static int uLength(String s) {
        String v = trimWhitespace(s);
        int result = 0;
        for (int i = 0, length = v.length(); i < length; i++) {
            result += v.charAt(i) <= 255 ? 1 : 2;
        }
        return result;
    }

    public static String truncateANSI(String src, int length) {
        return truncateANSI(src, length, "...");
    }

    public static String truncateANSI(String src, int length, String end) {
        String v = trimWhitespace(src);
        for (int i = 0, len = 0; i < v.length(); i++) {
            len += v.charAt(i) <= 255 ? 1 : 2;
            if (len > length) {
                return v.substring(0, i) + trimWhitespace(end);
            }
        }
        return v;
    }

    @SafeVarargs
    public static <T> String join(T... elements) {
        return join((Object[])elements, (String)null);
    }

    public static String join(Object[] array, char separator) {
        return array == null?null:join(array, separator, 0, array.length);
    }

    public static String join(Object[] array, char separator, int startIndex, int endIndex) {
        if(array == null) {
            return null;
        } else {
            int noOfItems = endIndex - startIndex;
            if(noOfItems <= 0) {
                return "";
            } else {
                StringBuilder buf = new StringBuilder(noOfItems * 16);

                for(int i = startIndex; i < endIndex; ++i) {
                    if(i > startIndex) {
                        buf.append(separator);
                    }

                    if(array[i] != null) {
                        buf.append(array[i]);
                    }
                }

                return buf.toString();
            }
        }
    }

    public static String join(Object[] array, String separator) {
        return array == null?null:join(array, separator, 0, array.length);
    }

    public static String join(Object[] array, String separator, int startIndex, int endIndex) {
        if(array == null) {
            return null;
        } else {
            if(separator == null) {
                separator = "";
            }

            int noOfItems = endIndex - startIndex;
            if(noOfItems <= 0) {
                return "";
            } else {
                StringBuilder buf = new StringBuilder(noOfItems * 16);

                for(int i = startIndex; i < endIndex; ++i) {
                    if(i > startIndex) {
                        buf.append(separator);
                    }

                    if(array[i] != null) {
                        buf.append(array[i]);
                    }
                }

                return buf.toString();
            }
        }
    }

    public static String toString(Object obj) {
        return obj == null?"":obj.toString();
    }

    public static String join(Iterator<?> iterator, char separator) {
        if(iterator == null) {
            return null;
        } else if(!iterator.hasNext()) {
            return "";
        } else {
            Object first = iterator.next();
            if(!iterator.hasNext()) {
                return toString(first);
            } else {
                StringBuilder buf = new StringBuilder(256);
                if(first != null) {
                    buf.append(first);
                }

                while(iterator.hasNext()) {
                    buf.append(separator);
                    Object obj = iterator.next();
                    if(obj != null) {
                        buf.append(obj);
                    }
                }

                return buf.toString();
            }
        }
    }

    public static String join(Iterator<?> iterator, String separator) {
        if(iterator == null) {
            return null;
        } else if(!iterator.hasNext()) {
            return "";
        } else {
            Object first = iterator.next();
            if(!iterator.hasNext()) {
                return toString(first);
            } else {
                StringBuilder buf = new StringBuilder(256);
                if(first != null) {
                    buf.append(first);
                }

                while(iterator.hasNext()) {
                    if(separator != null) {
                        buf.append(separator);
                    }

                    Object obj = iterator.next();
                    if(obj != null) {
                        buf.append(obj);
                    }
                }

                return buf.toString();
            }
        }
    }

    public static String join(Iterable<?> iterable, char separator) {
        return iterable == null?null:join((Iterator)iterable.iterator(), separator);
    }

    public static String join(Iterable<?> iterable, String separator) {
        return iterable == null?null:join((Iterator)iterable.iterator(), separator);
    }

    public static boolean isNotEmpty(String s) {
        return !isEmpty(s);
    }
}
